
package de.fhhof.brst.dao;

import java.io.Serializable;
import java.util.List;
import javax.faces.context.FacesContext;
import org.hibernate.Query;
import org.hibernate.Session;
import de.fhhof.brst.data.Booking;
import de.fhhof.brst.data.Event;
import de.fhhof.brst.data.Ticket;
import de.fhhof.brst.data.User;
public class HibernateEventDAO implements Serializable, EventDAO {
  /**
   * 
   */
  private static final long serialVersionUID =  4993966311741815174L;

  private Session session;

  public HibernateEventDAO() {

  }

  @Override
  public int createEvent(de.fhhof.brst.data.Event event) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.save(event);

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
		return 0;
  }

  @Override
  public int deleteEvent(de.fhhof.brst.data.Event event) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.delete(event);
		
		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
		return 0;
  }

  @Override
  public int editEvent(de.fhhof.brst.data.Event event) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.update(event);
		
		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
		return 0;
  }

  @Override
  public int bookEvent(de.fhhof.brst.data.Booking booking) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.save(booking);
		
		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
		return 0;
  }

  @Override
  public List<de.fhhof.brst.data.Event> getAllEvents() {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		List events = session.createQuery("from Event").list();

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();

		return events;
  }

  @Override
  public de.fhhof.brst.data.Event getEvent(long eventID) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = (Query) session.createQuery("from Event where id = ?").setLong(0, eventID);
		
		List events = query.list();
		Event event = (Event) events.get(0);

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();

		return event;
  }

  @Override
  public List<de.fhhof.brst.data.Booking> getBookings(long userID) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = (Query) session
		.createQuery("from Booking where user_id = ?").setLong(0, userID);

		List bookings = query.list();

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();

		return bookings;
  }

  @Override
  public List<de.fhhof.brst.data.Ticket> getTickets(long userID) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = (Query) session
		.createQuery("from Ticket where customer_id = ?").setLong(0, userID);

		List tickets = query.list();

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();

		return tickets;
  }

  public static HibernateEventDAO getHibernateEventDAO()
  {
		return new HibernateEventDAO();
  }

  @Override
  public void deleteBooking(de.fhhof.brst.data.Booking deleteBooking) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.delete(deleteBooking);
		
		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
  }

  @Override
  public List<de.fhhof.brst.data.Booking> getAllBookings(long eventID) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = (Query) session
		.createQuery("from Booking where event_id = ?").setLong(0, eventID);

		List bookings = query.list();

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();

		return bookings;
  }

  @Override
  public void saveTicket(de.fhhof.brst.data.Ticket ticket) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		session.save(ticket);

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
  }

  @Override
  public List<de.fhhof.brst.data.Ticket> getAllTickets(long id) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = (Query) session
		.createQuery("from Ticket where event_id = ?").setLong(0, id);

		List tickets = query.list();

		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();

		return tickets;
  }

  @Override
  public void deleteTicket(de.fhhof.brst.data.Ticket ticket) {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.delete(ticket);
		
		session.getTransaction().commit();
		HibernateUtil.getSessionFactory().close();
  }

}
